﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GameSPK.Helpers;

namespace GameSPK.DAO
{
    class NguoiChoiDAO
    {
        public static bool ThemNguoiChoiCuocChoi(ObservableCollection<INguoiChoi> taphop, General cuocthi)
        {
            bool kq = false;
            if (taphop == null)
                return kq;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            foreach (dataNguoiChoi dataNC in taphop)
            {
                if (dataNC.IsEnabled == true)
                {

                    NguoiChoi_CuocThi ng_cthi = new NguoiChoi_CuocThi();
                    ng_cthi.MaNguoiChoi = dataNC.MaNguoiChoi;
                    ng_cthi.MaCuocChoi = cuocthi.MaCuocChoi;
                    db.NguoiChoi_CuocThis.InsertOnSubmit(ng_cthi);
                }
            }
            try
            {
                db.SubmitChanges();
            }
            catch
            {
                return false;
            }
            kq = true;
            return kq;
        }
        public static bool UpdateNguoiChoiPhanThi(ObservableCollection<INguoiChoi> taphop, General cuocthi)
        {
            bool kq = false;
            if (taphop == null)
                return kq;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            foreach (dataNguoiChoi dataNC in taphop)
            {
                if (dataNC.IsEnabled == true)
                {
                    var nguoichoiupdate = (from chupdate in db.NguoiChois
                                        where chupdate.MaNguoiChoi == dataNC.MaNguoiChoi
                                        select chupdate).Single();
                    nguoichoiupdate.TenNguoiChoi = dataNC.TenNguoiChoi;
                    nguoichoiupdate.ThongTin = dataNC.ThongTin;
                    nguoichoiupdate.Video = dataNC.Video;
                    continue;
                }
                var cauhoi_cuocchoi = (from chch in db.NguoiChoi_CuocThis
                                       where chch.MaNguoiChoi == dataNC.MaNguoiChoi
                                       select chch).Single();

                db.NguoiChoi_CuocThis.DeleteOnSubmit(cauhoi_cuocchoi);
            }
            try
            {
                db.SubmitChanges();
            }
            catch
            {
                return false;
            }
            kq = true;
            return kq;
        }
        public static ObservableCollection<INguoiChoi> getNguoiChoiDaChon(General cuocthi)
        {

            ObservableCollection<INguoiChoi> lstNguoiChoi = new ObservableCollection<INguoiChoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var ng_cth = from NG in db.NguoiChoi_CuocThis
                         where NG.MaCuocChoi == cuocthi.MaCuocChoi
                         select NG;
            foreach (NguoiChoi_CuocThi nguoichoi_cuocthi in ng_cth)
            {
                var ngchoi = (from nguoichoi in db.NguoiChois
                              where nguoichoi.MaNguoiChoi == nguoichoi_cuocthi.MaNguoiChoi
                              select nguoichoi).Single();
                if (ngchoi != null)
                {
                    INguoiChoi inguoichoi = new dataNguoiChoi(ngchoi, true);

                    lstNguoiChoi.Add(inguoichoi);
                }
            }
            return lstNguoiChoi;
        }
        public static ObservableCollection<INguoiChoi> getNguoiChoiTheoTen(General cuocchoi, String name)
        {

            ObservableCollection<INguoiChoi> lstNguoiChoi = new ObservableCollection<INguoiChoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var ngchoi = from NG in db.NguoiChois
                         where NG.TenNguoiChoi.Contains(name)
                         select NG;
            var nguoichoi_cuocchoi = from ng_cc in db.NguoiChoi_CuocThis
                                     where ng_cc.MaCuocChoi == cuocchoi.MaCuocChoi
                                     select ng_cc;
            bool flag;
            foreach (NguoiChoi nguoichoi in ngchoi)
            {
                flag = true;
                foreach (NguoiChoi_CuocThi ngchoi_cchoi in nguoichoi_cuocchoi)
                {
                    if (nguoichoi.MaNguoiChoi == ngchoi_cchoi.MaNguoiChoi)
                    {
                        flag = false;
                        break;
                    }
                }
                if (!flag)
                    continue;
                INguoiChoi ingchoi = new dataNguoiChoi(nguoichoi, true);
                lstNguoiChoi.Add(ingchoi);
            }
            return lstNguoiChoi;
        }
        public static bool NhapNguoiChoi(NguoiChoi nguoichoi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                db.NguoiChois.InsertOnSubmit(nguoichoi);
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static bool ChonNguoiChoiCuocThi(NguoiChoi nguoichoi, General cuocthi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                NguoiChoi_CuocThi nguoichoi_cuocthi = new NguoiChoi_CuocThi();
                nguoichoi_cuocthi.MaCuocChoi = cuocthi.MaCuocChoi;
                nguoichoi_cuocthi.MaNguoiChoi = nguoichoi.MaNguoiChoi;
                db.NguoiChoi_CuocThis.InsertOnSubmit(nguoichoi_cuocthi);
                db.SubmitChanges();  
            }
            catch
            {
                result = false;
            }
            return result;
        }
    }
}
